package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.jieshuibao.mapper.WishMapper;
import com.jieshuibao.model.db.Wish;
import com.jieshuibao.model.db.WishExample;

@Component
public class WishStartNum implements ApplicationListener<ContextRefreshedEvent> {
	
	@Resource
	private WishMapper wishMapper;
	
	@Resource
	private RedisService redisService;
	
	@Resource
	private AdvisoryService advisoryService;
	
	@Resource
	private AdvisoryRecordingService advisoryRecordingService;
	
	@Resource
	private AdvisoryUserService advisoryUserService;

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(event.getApplicationContext().getParent() == null){
			WishExample wishExample = new WishExample();
			wishExample.setOrderByClause("orderNum desc");
			List<Wish> list = wishMapper.selectByExample(wishExample);
			for (Wish wish : list) {
				String id = wish.getId();
				Integer startNum = wish.getStartNum();
				String valueOf = String.valueOf(startNum);
				String string = redisService.get(id+"_code");
				if(string==null){
					redisService.set(id+"_code", valueOf, 0);
				}
				
			}
							
			/* ScheduledExecutorService service = Executors  
			             .newSingleThreadScheduledExecutor();
			 Runnable runnable = new Runnable() {  
		         public void run() {  
		        	
		        	 AdvisoryExample advisoryExample = new AdvisoryExample();
		        	 Criteria createCriteria = advisoryExample.createCriteria();
		        	 
		        	 createCriteria.andTypeEqualTo("2");
		        	 List<Advisory> list = advisoryService.getAdvisoryByExample(advisoryExample);
		        	for (Advisory advisory : list) {
		        		
		        		String uuid = advisory.getUuid();
		        		String onlyId = advisory.getOnlyId();
		        		String userRes = advisory.getUserRes();
		        		Integer money = advisory.getMoney();
		        		
		        		String string = redisService.get(uuid+"_end");
		        		AdvisoryRecording advisoryRecording = advisoryRecordingService.getAdvisoryRecordingByUuid(string);
		        		if(advisoryRecording!=null){
		        			
		        			Long chatTime = advisoryRecording.getChatTime();
		        			long endTime=chatTime+20*60*1000;
		        			long currentTimeMillis = System.currentTimeMillis();
		        			
		        			if(endTime<currentTimeMillis){
		        				//结束业务
		        				Advisory advisory2 = new Advisory();
		        				advisory2.setEndTime(currentTimeMillis);
		        				advisory2.setUuid(uuid);
		        				advisory2.setType("1");
		        				
		        				AdvisoryUserExample advisoryUserExample = new AdvisoryUserExample();
		        				com.jieshuibao.model.db.AdvisoryUserExample.Criteria createCriteria2 = advisoryUserExample.createCriteria();
		        				createCriteria2.andOnlyIdEqualTo(onlyId);
		        				List<AdvisoryUser> list1 = advisoryUserService.getAdvisoryUserByExample(advisoryUserExample);
		        				AdvisoryUser advisoryUser1=null;
		        				AdvisoryUser advisoryUser2=null;
		        				advisoryUser1 = list1.get(0);
		        				advisoryUser2 = list1.get(1);
		        				String userOne = advisoryUser1.getUserOne();
		        				advisoryUser1.setLastTime(currentTimeMillis);
		        				advisoryUser2.setLastTime(currentTimeMillis);
		        				if(userOne.equals(userRes)){
		        					advisoryUser1.setType("0");
		        					advisoryUser2.setType("1");
		        				}else {
		        					advisoryUser1.setType("1");
		        					advisoryUser2.setType("0");
		        				}
		        				advisoryUser1.setMessage("#@@&@@#");
		        				advisoryUser2.setMessage("#@@&@@#");
		        				
		        				AdvisoryRecording advisoryRecording2 = new AdvisoryRecording();
		        				String uuid1 = UUIDGenerator.getUUID();
		        				advisoryRecording2.setUuid(uuid1);
		        				advisoryRecording2.setAdvisoryId(uuid);
		        				advisoryRecording2.setOnlyId(onlyId);
		        				advisoryRecording2.setUserId(userRes);
		        				advisoryRecording2.setChatTime(currentTimeMillis);
		        				advisoryRecording2.setType("4");
		        				advisoryRecording2.setContent("#@@&@@#");
		        				advisoryRecordingService.addAdvisoryRecordUser(advisoryRecording2, advisoryUser1, advisoryUser2, advisory2, userRes, money);
		        				redisService.delete(uuid+"_end");
		        				redisService.delete(uuid+"_disagree");
		        				
		        			}
		        		}else {
							
		        			System.out.println("advisoryRecording是空");
						}
		        		
		        	}
		        	
		        	System.out.println("hello");
		         }  
		     };  
		     
		     // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间  
		     service.scheduleAtFixedRate(runnable, 10, 300, TimeUnit.SECONDS);  
		     
		
			*/

		}
	}

	

}
